const Common = {

    /**对象管理池 */
    poolManger: null,
    /**游戏配置信息 */
    GameConfig: {},
    isFrist: false,//第一次进入游戏
    /**是否展示幸运抽奖 */
    isLuckyFristShow: false,
    /**当前关卡 */
    currLevel: 1,
    /**完成数量 */
    finishCount: 0,
    /**首次点击屏幕时间 */
    startTouchTime: new Date().getTime(),

    puzzlePic: null,

    welfarePic:null,

    countDownTime :[
        { "id": 1, "time": 30 },
        { "id": 2, "time": 60 },
        { "id": 3, "time": 60 },
        { "id": 4, "time": 60 },
        { "id": 5, "time": 90 },
        { "id": 6, "time": 60 },
        { "id": 7, "time": 60 },
        { "id": 8, "time": 90 },
        { "id": 9, "time": 120 },
        { "id": 10, "time": 120 }
    ],
    /**最大能量数 */
    maxEnger:3,

    engerInterval:60,

    /**当前能量 */
    enger:0,
    /**上次获取能量时间 */
    engerTime:0,
    /**获取当前能量 */
    getEnger() {

        let str = cc.sys.localStorage.getItem('picturePuzzle_Enger');
        // console.log("缓存数据",str);
        if (!str) {
             Common.enger =Common.maxEnger;
            cc.sys.localStorage.setItem('picturePuzzle_Enger', JSON.stringify( Common.enger));
            return  Common.enger;
        } else {
            // console.log("JSON.parse(str)=== ",JSON.parse(str));
            Common.enger = JSON.parse(str);
            return Common.enger;
        }
    },
    /**
     * 设置能量总数
     * @param {*} enger 
     */
    setEnger(enger,type) {
        // console.log(type,enger);
        Common.enger = enger;
        cc.sys.localStorage.setItem('picturePuzzle_Enger', JSON.stringify(Common.enger));
    },

    setEngerTime(time) {
        Common.engerTime = time;
        cc.sys.localStorage.setItem('picturePuzzle_EngerTime', JSON.stringify(Common.engerTime));
    },

    getIsFrist() {
        let str = cc.sys.localStorage.getItem('picturePuzzle_IsFrist');
        // console.log("缓存数据",str);
        if (!str) {
            cc.sys.localStorage.setItem('picturePuzzle_IsFrist', JSON.stringify(true));
            return  false;
        } else {
            return JSON.parse(str);
        }
    },


    getEngerTime() {
        let time = cc.sys.localStorage.getItem('picturePuzzle_EngerTime');
        // console.log(time);
        // console.log("缓存数据",str);
        if (!time || time == "undefined"||time == "null") {
            Common.engerTime = new Date().getTime();
            cc.sys.localStorage.setItem('picturePuzzle_EngerTime', JSON.stringify(Common.engerTime));
            return Common.engerTime;
        } else {
            // console.log(time);
            // console.log("JSON.parse(str)=== ",JSON.parse(str));
            Common.engerTime = JSON.parse(time)
            return Common.engerTime;
        }
    },
    /**获取倒计时时间 */
    getCountDownTime(id) {
        for (let index = 0; index < Common.countDownTime.length; index++) {
            if (id == Common.countDownTime[index].id)
                return Common.countDownTime[index].time;

        }
    },

    /**
     * 
     * @param {*} filename 文件名称
     * @param {*} cb 加载回调
     */
    loadJson(filename, cb) {
        cc.resources.load(filename, function (err, object) {
            if (err) {
                // console.log("load json failed:" + err);
            } else {
                Common.GameConfig[filename] = object.json;
                // console.log("JsonFile " + filename, Common.GameConfig[filename])
                if (cb) cb();
            }
        });
    },
    /**
     * 加载Icon
     * @param {*} filename 
     * @param {*} cb 
     */
    loadIcon(filename, cb) {
        cc.resources.loadDir(filename, cc.SpriteFrame, function (err, atlas) {
            if (err) {
                // console.log("load SpriteAtlas failed:" + err);
            } else {
                // Common.GameConfig[filename] = object.json;
                Common.iconSpriteAtlas = atlas;
                // console.log("SpriteAtlas " + filename, atlas)
                if (cb) cb();
            }
        });
    },
    /**
     * 加载图片
     * @param {*} filename 
     * @param {*} cb 
     */
    loadImg(pathname, cb) {
        cc.resources.load(pathname, cc.SpriteFrame, function (err, SpriteFrame) {
            if (err) {
                // console.log("load SpriteFrame failed:" + err);
            } else {
                if (cb) cb(SpriteFrame)
            }
        });
    },
    /**
     * 
     * @param {*} type 获取当前类型装扮配置
     */
    getCurrLevelConfig(type, level) {
        Common.currLevelConfig = null;
        for (let i = 0; i < Common.GameConfig[type].length; i++) {
            if (Common.GameConfig[type][i].id == level) {
                Common.currLevelConfig = Common.GameConfig[type][i];
            }
        }
        return Common.currLevelConfig;
    },
    /**
     * 更具ID获取配置文件
     * @param {*} id 
     */
    getCurrdressUpByID(id) {
        for (let i = 0; i < Common.GameConfig["DressUp"].length; i++) {
            if (Common.GameConfig["DressUp"][i].id == id) {
                return Common.GameConfig["DressUp"][i];
            }
        }
    },
    /**适配分辨率默认高度适配，iphonex宽度适配 */

    setDesignResolution() {
        var canvas = cc.find("Canvas").getComponent(cc.Canvas);

        let winSize = cc.view.getDesignResolutionSize();//cc.winSize;
        let frameSize = cc.view.getFrameSize();
        // console.log(winSize, frameSize, cc.view.getDesignResolutionSize())

        if (winSize.width / winSize.height < frameSize.width / frameSize.height) {
            canvas.fitWidth = false;
            canvas.fitHeight = true;
            //cc.view.setResolutionPolicy(cc.ResolutionPolicy.FIXED_HEIGHT);
        } else {
            canvas.fitWidth = true;
            canvas.fitHeight = false;
            //cc.view.setResolutionPolicy(cc.ResolutionPolicy.FIXED_WIDTH);
        }
    },
    /**
     * 加载并创建指定预制
     * @param {*} bundleName 
     * @param {*} prefabName 
     * @param {*} cb 
     */
    createPrefab(bundleName, prefabName, cb) {
        let bundle = cc.assetManager.getBundle(bundleName);
        if (!bundle) {
            cc.assetManager.loadBundle(bundleName, function (err, bundle) {
                if (err) {
                    console.error("prefab 加载错误：", err);
                    return;
                }
                Common.loadPrefab(bundle, prefabName, cb);
            });
        } else {
            Common.loadPrefab(bundle, prefabName, cb);
        }
    },

    loadPrefab(bundle, prefabName, cb) {
        let levelPrefab = bundle.get(prefabName);
        if (levelPrefab) {
            if (cb) cb(levelPrefab);
        } else {
            bundle.load(prefabName, cc.Prefab, function (error, prefab) {
                if (error) {
                    cc.error(error);
                    return;
                }
                if (cb) cb(prefab);
            });
        }
    },

    /**
     * 生成 1~n之间顺序打乱的数组
     * @param {*} length 
     * @returns 
     */
    randArray(length) {
        var data = new Array();
        //创建数组 存放下标数
        var try1 = new Array();
        for (var i = 0; i < length; i++) {
            data[i] = i + 1;
            try1[i] = i;
        }
        //创建数组 生成随机下标数
        var try2 = new Array();
        for (var i = 0; i < length; i++) {
            try2[i] = try1.splice(Math.floor(Math.random() * try1.length), 1);
        }
        //创建数组，生成对应随机下标数的数组
        var try3 = new Array();
        for (var i = 0; i < length; i++) {
            try3[i] = data[try2[i]];
        }
        return try3;
    },
    /**数组转Json */
    arraytoJson(arr, keyName) {
        let myJson = {};
        for (let i = 0; i < arr.length; i++) {
            myJson[arr[i][keyName]] = arr[i];
        }
        return myJson;
    },
    distance(p1, p2) {
        var dx = Math.abs(p2.x - p1.x);
        var dy = Math.abs(p2.y - p1.y);
        // return Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
        return Math.pow(dx, 2) + Math.pow(dy, 2);
    },
    /**时间格式化 */
    timeformatting(time) {
        // let h = Math.floor(time / 3600) < 10 ? '0' + Math.floor(time / 3600) : Math.floor(time / 3600);
        let m = Math.floor((time / 60 % 60)) < 10 ? '0' + Math.floor((time / 60 % 60)) : Math.floor((time / 60 % 60));
        let s = Math.floor((time % 60)) < 10 ? '0' + Math.floor((time % 60)) : Math.floor((time % 60));
        // let str = h + ":" + m + ":" + s
        let str = m + ":" + s
        return str;
    },

}

export default Common;